/*
 * Copyright (C) 2010 Graham Allan
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package edu.strath.cis.grallan.findbugs.adaptiveranking.population;

import java.util.ArrayList;
import java.util.Collection;

import edu.strath.cis.grallan.findbugs.adaptiveranking.UserDesignationToPositivesMap;
import edu.strath.cis.grallan.findbugs.adaptiveranking.UserDesignationToPositivesMap.Positive;
import edu.umd.cs.findbugs.BugInstance;
import edu.umd.cs.findbugs.filter.Matcher;

/**
 * @author Graham Allan (grallan@cis.strath.ac.uk)
 */
public abstract class AbstractPopulation implements Population {

	protected Collection<BugInstance> populationAlerts;
	protected final UserDesignationToPositivesMap designationMapper;

	public AbstractPopulation(UserDesignationToPositivesMap designationMapper) {
		this.designationMapper = designationMapper;
		this.populationAlerts = new ArrayList<BugInstance>();
	}

	public double adjustmentFactor() {
		int truePositives = 0;
		int falsePositives = 0;

		for (BugInstance bug : populationAlerts) {
			Positive positive = designationMapper.getPositiveDesignationFromBug(bug);

			if (positive == Positive.TRUE_POSITIVE) {
				truePositives++;
			}

			if (positive == Positive.FALSE_POSITIVE) {
				falsePositives++;
			}
		}

		if (truePositives + falsePositives == 0)
			return 0.0d;

		double result = ((double) (truePositives - falsePositives)) / ((double) (truePositives + falsePositives));
		return result;
	}

	/**
	 * @throws PopulationException
	 *             when <code>bugInstance</code> is for a different package from <code>this</code>
	 *             population.
	 */
	public Population addAlert(BugInstance bugAlert) {
		checkAlert(bugAlert);
		populationAlerts.add(bugAlert);
		return this;
	}

	/**
	 * Called before a bug alert is added to a population. AbstractPopulation requires that an added
	 * alert is non-null. Subclasses should override to provide their own checks, and throw any
	 * necessary. {@link PopulationException}. To allow nulls to be added, override and remove call
	 * to <code>super.checkAlert()</code>. <br>
	 * <br>
	 * This method is used in determining the result of {@link #isValidAlert(BugInstance)}. No
	 * exceptions thrown means isValid returns true.
	 * 
	 * @see #isValidAlert(BugInstance)
	 * @throws PopulationException
	 *             if the alert is invalid for this population
	 */
	protected void checkAlert(BugInstance bugAlert) {
		if (bugAlert == null)
			throw new PopulationException(new NullPointerException("Added bug alert cannot be null"));
	}

	public int size() {
		return populationAlerts.size();
	}

	public int numberOf(Matcher bugMatcher) {
		int number = 0;
		for (BugInstance bug : populationAlerts) {
			if (bugMatcher.match(bug)) {
				number++;
			}
		}
		return number;
	}

	/**
	 * This should not need to be overriden by subclasses. Uses {@link #checkAlert(BugInstance)} to
	 * determine if a bug is valid, subclasses should override that method instead.
	 */
	public boolean isValidAlert(BugInstance bug) {
		try {
			checkAlert(bug);
		} catch (PopulationException pe) {
			return false;
		}
		return true;
	}

}